package com.zzb.servlet.product;

import com.alibaba.fastjson.JSONObject;
import com.zzb.DTO.ResultDTO;
import com.zzb.VO.ProductVO;
import com.zzb.service.ProductService;
import com.zzb.service.impl.ProductServiceImpl;
import com.zzb.util.Tool;

import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.nio.charset.StandardCharsets;

@WebServlet("/back/product/all")
public class ProductFindAllServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        //乱码处理
        req.setCharacterEncoding(StandardCharsets.UTF_8.toString());
        resp.setCharacterEncoding(StandardCharsets.UTF_8.toString());
        //得到前端数据
        String page = req.getParameter("page");
        String limit = req.getParameter("limit");
        String isShow = req.getParameter("isShow");
        String name = req.getParameter("name");
        String typeId = req.getParameter("typeId");
        String enable = req.getParameter("enable");


        //对前端数据进行封装
        ProductVO productVO = ProductVO.builder()
                .page(Integer.valueOf(page))
                .limit(Integer.valueOf(limit))
                .name(Tool.nullToStr(name))
                .isShow(Integer.valueOf(Tool.nullToNum(isShow)))
                .typeId(Integer.valueOf(Tool.nullToNum(typeId)))
                .enable(Integer.valueOf(Tool.nullToNum(enable)))
                .build();
        //调用服务逻辑
        ProductService productService = new ProductServiceImpl();

        //得到返回值
        ResultDTO resultDTO = productService.findAll(productVO);
        //响应
        resp.getWriter().println(JSONObject.toJSONString(resultDTO));
    }
}
